package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelsQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravlesService;
import cn.wolfcode.luowowo.cache.service.IUserRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.annoation.LoginCheck;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.naming.directory.SearchResult;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {
    @Reference
    private IUserRedisService userRedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravlesService travlesService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private ISearchService searchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IStrategySearchService strategySearchService;

    @RequestMapping("")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
        //游记推荐的前4篇
        List <TravelCommend> travelCommends = travelCommendService.list();
        model.addAttribute( "tcs", travelCommends.size() > 5 ? travelCommends.subList( 0, 5 ) : travelCommends );
        //攻略推荐的第一个
        StrategyCommend sc = strategyCommendService.list().get( 0 );
        model.addAttribute( "sc", sc );
        //游记查询的qo,已经设置了默认的orderby条件,默认按照最热进行排序

        return "index/index";

    }

    //在加载页面后,另外发送一个请求,查询分页的pageInfo
    @RequestMapping("index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelsQuery query) {
        PageInfo page = travlesService.query( query );
        model.addAttribute( "pageInfo", page );
        return "index/travelPageTpl";
    }

    //根据不同的查询条件返回不同的页面
    @RequestMapping("/q")
    public String q(@ModelAttribute("qo") SearchQueryObject qo, Model model) {
        System.out.println( qo.getType() );
        //按照目的地查询
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DEST: {
                return queryByDestName( qo, model );
            }
            case SearchQueryObject.SEARCH_TYPE_USER: {
                return queryByUserName( qo, model );
            }
            case SearchQueryObject.SEARCH_TYPE_TRAVEL: {
                return queryByTravel( qo, model );
            }
            case SearchQueryObject.SEARCH_TYPE_STRATEGY: {
                return queryByStrategy( qo, model );
            }
            default: {
                return queryAll( qo, model );
            }
        }


    }

    private String queryAll(SearchQueryObject qo, Model model) {
        Page <DestinationTemplate> destPage = searchService.searchWithHighlight( DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "info", "name" );
        Page <StrategyTemplate> stsPage = searchService.searchWithHighlight( StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary" );
        Page <TravelTemplate> tsPage = searchService.searchWithHighlight( TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary" );
        Page <UserInfoTemplate> usPage = searchService.searchWithHighlight( UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName" );
        List <DestinationTemplate> dests = destPage.getContent();
        List <StrategyTemplate> sts = stsPage.getContent();
        List <TravelTemplate> ts = tsPage.getContent();
        List <UserInfoTemplate> us = usPage.getContent();
        int count = dests.size() + sts.size() + ts.size() + us.size();
        us = us.size() > 5 ? us.subList( 0, 5 ) : us;
        dests = dests.size() > 5 ? dests.subList( 0, 5 ) : dests;
        sts = sts.size() > 5 ? sts.subList( 0, 5 ) : sts;
        ts = ts.size() > 5 ? ts.subList( 0, 5 ) : ts;
        SearchResultVO vo = new SearchResultVO();
        vo.setDests( dests );
        vo.setTravels( ts );
        vo.setStrategys( sts );
        vo.setUsers( us );
        vo.setTotal( Long.parseLong( count+"") );
        model.addAttribute( "data", vo );

        return "index/searchAll";
    }

    private String queryByStrategy(SearchQueryObject qo, Model model) {
        //攻略是全文搜索
        Page <StrategyTemplate> page = searchService.searchWithHighlight( StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary" );
        model.addAttribute( "page", page );
        return "index/searchStrategy";
    }

    private String queryByTravel(SearchQueryObject qo, Model model) {
        Page <TravelTemplate> page = searchService.searchWithHighlight( TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary" );
        model.addAttribute( "page", page );
        return "index/searchStrategy";
    }

    private String queryByUserName(SearchQueryObject qo, Model model) {
        Page <UserInfoTemplate> page = searchService.searchWithHighlight( UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName" );
        model.addAttribute( "page", page );
        return "index/searchUser";
    }

    private String queryByDestName(SearchQueryObject qo, Model model) {

        int count = 0;
        //将这个目的地的名称查询出来
        DestinationTemplate dest = destinationSearchService.queryByDestName( qo.getKeyword() );
        if (dest != null) {
            //查询该目的地下的游记
            List <StrategyTemplate> sts = strategySearchService.queryByDestName( qo.getKeyword() );
            count = sts.size();
            sts = sts.size() > 5 ? sts.subList( 0, 5 ) : sts;
            //查游记
            List <TravelTemplate> ts = travelSearchService.queryByDestName( qo.getKeyword() );
            count = count + ts.size();
            ts = ts.size() > 5 ? ts.subList( 0, 5 ) : ts;
            //查询该目的下的所有的用户
            List <UserInfoTemplate> us = userInfoSearchService.queryByDestName( qo.getKeyword() );
            count = count + sts.size();
            sts = sts.size() > 5 ? sts.subList( 0, 5 ) : sts;
            SearchResultVO vo = new SearchResultVO();

            vo.setTravels( ts );
            vo.setStrategys( sts );
            vo.setUsers( us );
            model.addAttribute( "data", vo );
        }
        model.addAttribute( "dest", dest );
        return "index/searchDest";
    }

}
